﻿using MongoDB.Bson;
using MongoDB.Driver;

namespace WebProject.Service;

public class MongodbService {
    // 腾讯云服务器的MongoDB服务
    private const string ConnectionString
        = "mongodb://root:123456@111.229.78.126:27017";
    // 数据库名称
    private const string DbName = "linkFileManage";
    // 表名
    private const string LinkFileCollectionName = "linkFile";

    // 获取数据库链接
    private MongoClient? _connection;

    // 初始化数据库链接
    private MongoClient Connection =>
        _connection ??= new MongoClient(ConnectionString);

    public BsonDocument GetDatabase() {
        var collection = Connection.GetDatabase(DbName).GetCollection<BsonDocument>(LinkFileCollectionName);
        var filter = Builders<BsonDocument>.Filter.Eq("name", "ocean");
        return collection.Find(filter).First();
    }
}